class MerchantController < ApplicationController
  layout 'main'
  
  before_filter :authorize, :except => [:login, :try_login, :logout, :new, :create, :check_username]
  
  def index
	render :action => 'login'
  end
  
  def new
    @merchant = Merchant.new
  end
  
  def create
    @merchant = Merchant.new(params[:merchant])
    if request.post? and @merchant.save
      flash.now[:notice] = "商户 #{@merchant.username} 已经成功创建。"
      @merchant = Merchant.new
    else
      render :action => 'new'
    end
  end
  
  def login
		if(session[:merchant])
			redirect_to :action => 'home'
		end
		@merchant = Merchant.new
		@merchant.username = params[:username]
  end
  
  def try_login
	ps = params[:merchant]
	merchant = Merchant.login(ps[:username], ps[:password])
	unless merchant.nil?
		# 修改上次登录时间
		merchant.last_login_time = Time.now
		session[:merchant] = merchant
		redirect_to :action => 'home'
	else
		flash[:notice] = "用户名或者密码错误"
		#为了登录的时候保持上次的用户名
		redirect_to :action => 'login', :username => ps[:username]
	end
  end
  
   def logout
		session[:merchant] = nil
		flash[:notice] = "您已经成功退出"
		redirect_to :action => 'login'
   end
  
  def home
	@stores = Store.find(:all, :conditions => ['merchant_id = ? ', session[:merchant].id])
  end
  
  def	edit
	merchant = session[:merchant]
	@merchant = Merchant.find(merchant.id)
  end
  
  def update
	merchant = session[:merchant]
	pm = params[:merchant]
	merchant.real_name = pm[:real_name]
	merchant.password = pm[:password]
	merchant.password_confirmation = pm[:password_confirmation]
	merchant.update
	flash[:notice] = "更新成功"
	redirect_to :action => 'edit'
  end
  
  def	new_store
	@merchant = session[:merchant]
	@store = Store.new
  end
  
  def create_store
	@store = Store.new(params[:store])
	@store.merchant_id = session[:merchant].id
	if request.post? and @store.save
      flash.now[:notice] = "快印店 #{@store.name} 已经成功创建。"
    else
      render :action => 'new_store'
    end
  end
  
  def	edit_store
	@merchant = session[:merchant]
	@store = find_store(params[:id], @merchant.id)
	# 试图获取不属于自己的商户信息
	if @store.nil?
		redirect_to :action => 'logout'
	end
  end
  
  def update_store
	@store = Store.find(params[:id])
	if @store.update_attributes(params[:store])
		flash[:notice] = "快印店信息成功修改"
    else
		render :action => 'edit_store'
    end
  end
  
  def disable_store
	change_store_state true
	redirect_to :action => 'home'
  end
  
  def enable_store
	change_store_state false
	redirect_to :action => 'home'
  end
  
  def view_store
	@merchant = session[:merchant]
	@store = find_store(params[:id], @merchant.id)
	# 试图获取不属于自己的商户信息
	if @store.nil?
		redirect_to :action => 'logout'
	end
  end
  
  def check_username
    unless params[:username].blank?
      merchant = Merchant.find_by_username(params[:username])
      if merchant.nil?
        render :update do |page|
          page.replace_html 'username_message', "用户名可以使用"
		  page['commit'].disabled = false
        end
      else
        render :update do |page|
          page.replace_html 'username_message', "该用户名不可以使用，请选择其他用户名"
		  page['commit'].disabled = true
        end
      end
    end
  end
  
  private
  # 检查当前操作者是否已经登录过
  def authorize
    if session[:merchant].nil?
      redirect_to :action => 'login'
    end
  end
  
  # 改变商店的激活状态
  def change_store_state(state)
	@store = Store.find(params[:id])
	@store.disabled = state
	@store.update
  end
  
  def find_store(store_id, merchant_id)
	@store = Store.find(:first, :conditions => ['store_id = ? AND merchant_id = ? ', store_id, merchant_id])
  end
end
